package core;

import java.util.StringTokenizer;

public class Localmessage
{
	///
	/** General Declaration **/
	String message;
	String messageType;
	String itemID;
	int BnumberOfItems;
	int CnumberOfItems;
	boolean state;
	/**
	 * This method is used to genrate the message that will be sent.
	 * @return
	 */
	public String constructMessage()
	{
		this.message = "";
		this.message = this.message + this.getMessageType() + ":";
		this.message = this.message + this.getItemID() + ":";
		this.message = this.message + this.getNumberOfItems() + ":";
		this.message = this.message + this.isState() + ":";
		this.message = this.message + this.getCnumberOfItems() + ":";
		return message; 
	}
	
	/**
	 * this method is used to parse the message.
	 * @param msg
	 * @return
	 */
	  public void parseMessage(String msg) {
	      System.out.println(msg);
	      //parse the received message based on delimeter @
	      StringTokenizer st = new StringTokenizer(msg, ":");
	      int index=0;
	      //Message m=new Message();
	      while (st.hasMoreTokens()) 
	      {
	          index++;
	          String token = st.nextToken();
	          switch(index)
	          {
	          case 1:
	        	  this.setMessageType(token);
	        	  break;
	          case 2:
	        	  this.setItemID(token);
	        	  break;
	          case 3:
	        	  //System.out.println(token);
	        	  //String sn = token;
	        	  //int sni = Integer.parseInt(sn);
	        	  this.setNumberOfItems(Integer.parseInt(token));
	        	  break;
	          case 4:
	        	  if(token == "true")
	        	  {
	        		  this.setState(true);
	        	  }
	        	  else
	        	  {
	        		  this.setState(false);
	        	  }
	        	  break;
	          case 5:
	        	  //System.out.println(token);
	        	  //String s = token;
	        	  //int si = Integer.parseInt(s);
	          	this.setCnumberOfItems(Integer.parseInt(token));
	          break;
	          }
	      }
	  }
	
	public String getMessage() 
	{
		return message;
	}
	public void setMessage(String Message) 
	{
		this.message = Message;
	}
	public String getMessageType() 
	{
		return messageType;
	}
	public void setMessageType(String messageType) 
	{
		this.messageType = messageType;
	}
	public String getItemID() 
	{
		return itemID;
	}
	public void setItemID(String itemID) 
	{
		this.itemID = itemID;
	}
	public int getNumberOfItems() 
	{	
		return BnumberOfItems;
	}
	public void setNumberOfItems(int numberOfItems) 
	{
		this.BnumberOfItems = numberOfItems;
	}
	public String isState() 
	{
		if(state == true)
		{
			return "true";
		}
		return "false";
	}
	public void setState(boolean state) 
	{
		this.state = state;
	}

	public int getCnumberOfItems() {
		return CnumberOfItems;
	}

	public void setCnumberOfItems(int cnumberOfItems) {
		CnumberOfItems = cnumberOfItems;
	}
	
}
